using System;
using System.Collections.Generic;
using ArtificialIntelligence.Racing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNA_2DFramwork;
using FarseerGames.FarseerPhysics;

namespace XnaGame
{
    public class Game3 :Game
    {
        public GraphicsDeviceManager graphics;
        public SpriteBatch spriteBatch;
        City city;
        RacePlayer player;
        Racer racer;
        Police police;
        ControlPlayer PC;
        public Game3()
        {
            graphics = new GraphicsDeviceManager(this);
            VisualObject._lVisualObject = new List<VisualObject>();
            Content.RootDirectory = "Content";
        }
        protected override void Initialize()
        {
            graphics.PreferredBackBufferWidth = graphics.GraphicsDevice.DisplayMode.Width;
            graphics.PreferredBackBufferHeight = graphics.GraphicsDevice.DisplayMode.Height;
            graphics.IsFullScreen= false;
            graphics.ApplyChanges();
                base.Initialize();
        }

        private PhysicsSimulator physicsSimulator;
        protected override void LoadContent()
        {
            physicsSimulator = new PhysicsSimulator();
            spriteBatch = new SpriteBatch(GraphicsDevice);

            city = new City(this,"street","Flag",physicsSimulator);
            VisualObject._lVisualObject.Add(city);

            player = new RacePlayer(this, new Vector2(570, 550), "playerCar", physicsSimulator);
            VisualObject._lVisualObject.Add(player);

            racer = new Racer(this, new Vector2(300, 350), "RacerCar", "GameOver", physicsSimulator);
            VisualObject._lVisualObject.Add(racer);

            police = new Police(this, new Vector2(200, -10000), this.player, "RacerCar","GameOver",physicsSimulator);
            VisualObject._lVisualObject.Add(police);

            PC = new ControlPlayer(player, city, this);
            RandomClass.Randomize(VisualObject._lVisualObject, this, physicsSimulator);
       
        }
        protected override void Update(GameTime gameTime)
        {
            if ((City.CurrentLenght < City.RacerLenght))
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    base.Update(gameTime);

                foreach (VisualObject Obj in VisualObject._lVisualObject)
                    Obj.Update();
                physicsSimulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);
                PC.Key();
            }
        }
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);
            foreach (VisualObject obj in VisualObject._lVisualObject)
                obj.Draw();
            base.Draw(gameTime);
        }
    }
}